#include "LuaPacket.h"
#include "mystdafx.h"
#include "ClientPacket.h"
#include "SerialBuffer.h"
#include "DvSystem.h"


static CClientPacket * g_pClientPacket = NULL;

LuaPacket::LuaPacket(DWORD len)
{
	g_pClientPacket = new  CClientPacket(len);
}

LuaPacket::LuaPacket(const BYTE* buf, int len)
{
	g_pClientPacket = new  CClientPacket(buf, len);
}


LuaPacket::~LuaPacket()
{
	if (g_pClientPacket)
	{
		g_pClientPacket->Destroy(true);
		CC_SAFE_DELETE(g_pClientPacket);
	}

}

void LuaPacket::Init(const BYTE* buf, int nBufLen)
{
	g_pClientPacket->Init(buf, nBufLen);
}

DWORD LuaPacket::GetPacketSize() const
{
	return g_pClientPacket->GetPacketSize();
}

WORD LuaPacket::GetCmd() const
{
	return g_pClientPacket->GetCmd();
}

DWORD LuaPacket::GetFlags() const
{
	return g_pClientPacket->GetFlags();
}

void LuaPacket::MakePacket()
{
	g_pClientPacket->MakePacket();
}

void LuaPacket::Copy(const BYTE* buf, DWORD len)
{
	g_pClientPacket->Copy(buf, len);
}

void LuaPacket::Append(const BYTE* buf, DWORD len)
{
	g_pClientPacket->Append(buf, len);
}
DWORD LuaPacket::Size() const
{
	return g_pClientPacket->Size();
}

BYTE* LuaPacket::Buffer() const
{
	return g_pClientPacket->Buffer();
}

BYTE LuaPacket::LuaReadByte()
{
	static BYTE value;
	(*g_pClientPacket) >> value;

	return value;
}

WORD LuaPacket::LuaReadWord()
{
	static WORD value;
	(*g_pClientPacket) >> value;

	return value;
}

WORD LuaPacket::LuaReadLong()
{
	static long value;
	(*g_pClientPacket) >> value;

	return value;
}

DWORD LuaPacket::LuaReadDword()
{
	static DWORD value;
	(*g_pClientPacket) >> value;

	return value;
}

UINT64 LuaPacket::LuaReadUINT64()
{
	static UINT64 value;
	(*g_pClientPacket) >> value;

	return value;
}

INT64 LuaPacket::LuaReadINT64()
{
	static INT64 value;
	(*g_pClientPacket) >> value;

	return value;
}

float LuaPacket::LuaReadFloat()
{
	static float value;
	(*g_pClientPacket) >> value;

	return value;
}

double LuaPacket::LuaReadDouble()
{
	static double value;
	(*g_pClientPacket) >> value;

	return value;
}

int LuaPacket::LuaReadInt()
{
	static int value;
	(*g_pClientPacket) >> value;

	return value;
}

bool LuaPacket::LuaReadBool()
{
	static bool value;
	(*g_pClientPacket) >> value;

	return value;
}

std::string LuaPacket::LuaReadString(UINT16 len)
{
	char *dest = new char[len+1];
	memset(dest, 0, len + 1);
	(*g_pClientPacket).Popup((BYTE*)dest, len);
	std::string result = dest;
	delete[]dest;

	return result;
}

std::string  LuaPacket::LuaReadWString(UINT16 len)
{
	char *dest = new char[len*2 + 1];
	memset(dest, 0, len * 2 + 1);
	(*g_pClientPacket).Popup((BYTE*)dest, len * 2);
//	CCLOG("===LuaPacket::LuaReadWString===%s===\n", dest);
	std::string result = DvSystem::ws2s((wchar_t*)dest);
//	CCLOG("===LuaPacket::LuaReadWString=result==%s===\n", result);
	delete[]dest;

	return result;
}

BOOL LuaPacket::LuaWriteByte(BYTE value)
{
	(*g_pClientPacket) << (BYTE)value;
	return true;
}

BOOL LuaPacket::LuaWriteWord(WORD value)
{
	(*g_pClientPacket) << (WORD)value;
	return true;
}

BOOL LuaPacket::LuaWriteLong(long value)
{
	(*g_pClientPacket) << (long)value;
	return true;
}

BOOL LuaPacket::LuaWriteDword(DWORD value)
{
	(*g_pClientPacket) << (DWORD)value;

	return true;
}

BOOL LuaPacket::LuaWriteUINT64(UINT64 value)
{
	(*g_pClientPacket) << (UINT64)value;
	return true;
}

BOOL LuaPacket::LuaWriteINT64(INT64 value)
{
	(*g_pClientPacket) << (INT64)value;
	return true;
}

BOOL LuaPacket::LuaWriteFloat(float value)
{
	(*g_pClientPacket) << (float)value;
	return true;
}

BOOL LuaPacket::LuaWriteDouble(double value)
{
	(*g_pClientPacket) << (double)value;
	return true;
}

BOOL LuaPacket::LuaWriteInt(int value)
{
	(*g_pClientPacket) << (int)value;
	return true;
}

BOOL LuaPacket::LuaWriteBool(bool value)
{
	(*g_pClientPacket) << (bool)value;

	CCLOG("===LuaPacket::LuaWriteBool==11111111=%d==\n", (*g_pClientPacket).Size());

	CCLOG("===LuaPacket::LuaWriteBool==11111111=%s==\n", (*g_pClientPacket).Buffer());

	return true;
}

BOOL LuaPacket::LuaWriteString(std::string value, UINT16 len)
{

	(*g_pClientPacket).Append((BYTE *)value.c_str(), len);

	return true;
}

BOOL LuaPacket::LuaWriteWString(std::string value, UINT16 len)
{

//	CCLOG("===LuaPacket::LuaWriteWString==11111111=%d==\n", (*g_pClientPacket).Size());

	std::wstring str = DvSystem::s2ws(value);
	
	(*g_pClientPacket).Append((BYTE *)str.c_str(), len * 2);
	

//	CCLOG("===LuaPacket::LuaWriteWString==22222222=%d==\n", (*g_pClientPacket).Size());
 

	return true;
}


